<?php

namespace app\admin\model;

use think\Model;


class Project_2020Real extends Model
{





    // 表名
    protected $name = 'project_2020_real';

     protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'access_time_text',
        'sign_time_text',
        'framework_agreement_time_text',
        'invesment_agreement_time_text',
        'register_time_text',
        'start_construction_time_text',
        'create_time_text',
        'update_time_text',
        'tmp_access_time_text',
        'tmp_sign_time_text',
        'tmp_framework_agreement_time_text',
        'tmp_invesment_agreement_time_text',
        'tmp_register_time_text',
        'tmp_start_construction_time_text',
        'carrier_id_relation_unit',
        'tmp_carrier_id_relation_unit',
        'intention_time_text',
        'tmp_intention_time_text'
    ];

    // public function __construct($data = [],$year='')
    // {

    //     $year = $year ?: date('Y');
    //     // $this -> name = 'project_'.$year;

    //     parent::__construct($data);
    // }

    protected static function init()
    {
        self::afterUpdate(function ($row) {

            $library_id = $row['library_id'];
            // var_dump($row->toArray());
            $where['table_name'] = 'project_2020_real';
            $where['status'] = 1;
            // $where['item_library'] = ['in',"0,{$library_id}"];
            $fields_list = Fields::where($where)->select();
            // pr(collection($fields_list)->toArray());
            $status = 0;
            $reject_status = 0;
            $required_arr = [];
            foreach ($fields_list as $key => $value) {
                if(isset($row["tmp_{$value['item_id']}"]) && $row["tmp_{$value['item_id']}_status"] == STATUS_SUBMIT)
                {
                    // echo $value['item_id'];
                    $status ++;
                }

                if(isset($row["tmp_{$value['item_id']}"]) && $row["tmp_{$value['item_id']}_status"] == STATUS_REJECT)
                {
                    // echo $value['item_id'];
                    $reject_status ++;
                }

                $item_required = $value['item_required'] ? json_decode($value['item_required'],true):'';
                if(isset($row['library_id']) && $row['library_id'] && $item_required )
                {
                    $is_required = $item_required[$row['library_id']]['is_required'];

                    if($is_required == '1'   && $value['item_class'] != 'capital' && $value['item_class'] != 'agreement')
                    {

                        if(!$row[$value['item_id']] && !$row['tmp_'.$value['item_id']])
                            $required_arr[] = $value['item_label'];

                    }
                }
            }
            if(!$row['domestic_capital'] && !$row['tmp_foreign_capital'] && !$row['tmp_domestic_capital'] && !$row['foreign_capital'])
                $required_arr[] = '内外资(至少填一项)';



            if(isset($row['library_id']) && $row['library_id']==2)
            {
                if((!$row['carrier_id'] && $row['carrier_id'] != 5 && !$row['carrier_id_relation']) && ($row['tmp_carrier_id'] && $row['tmp_carrier_id'] != 5 && !$row['tmp_carrier_id_relation']))
                    $required_arr[] = '载体面积';

                if(($row['is_access'] == '是' &&  !$row['access_time'])|| ($row['tmp_is_access'] == '是' &&  !$row['tmp_access_time']))
                    $required_arr[] = '准入时间';
                if(($row['is_land'] == '是' &&  !$row['land_time']) ||  ($row['tmp_is_land'] == '是' &&  !$row['tmp_land_time']))
                    $required_arr[] = '拿地时间';

                if(!$row['framework_agreement_time'] && !$row['invesment_agreement_time'] && !$row['tmp_framework_agreement_time'] && !$row['tmp_invesment_agreement_time'])
                    $required_arr[] = '框架、正式协议(至少填一项)';
            }


            $status = $status ? 1:0;
            $reject_status = $reject_status ? 1:0;
            $required_text = implode(",",$required_arr);

            // pr($required_arr);

            $progress_status_id = 0;
            $progress_status_name = '';
            if(isset($row['framework_agreement_time']) && $row['framework_agreement_time'] && $row['framework_agreement_time']!=-100 && $row['framework_agreement_time'] < time()){
                $progress_status_id = 4;
                $progress_status_name = '框架协议';
            }

            if(isset($row['invesment_agreement_time']) && $row['invesment_agreement_time'] && $row['invesment_agreement_time']!=-100 && $row['invesment_agreement_time'] < time())
            {
                $progress_status_id = 5;
                $progress_status_name = '正式协议';
            }

            if(isset($row['register_time']) && $row['register_time'] && $row['register_time']!=-100 && $row['register_time'] < time()){
                $progress_status_id = 6;
                $progress_status_name = '注册';
            }
            if(isset($row['land_time']) && $row['land_time'] && $row['land_time']!=-100 && $row['land_time'] < time()){
                $progress_status_id = 7;
                $progress_status_name = '拿地';

            }
            // echo  $row['start_construction_time'];
            if(isset($row['start_construction_time']) && $row['start_construction_time'] && $row['start_construction_time']!=-100 && $row['start_construction_time'] < time()){
                $progress_status_id = 8;
                $progress_status_name = '开工';

            }

            $is_important = 0;
            $major_activity = Activity::where(['is_important'=>1])->column('id');
            if($row['project_source_ids'] )
            {
                $project_source_ids = explode(",",$row['project_source_ids']);
                $result = array_intersect($major_activity,$project_source_ids);
                if($result)
                    $is_important = 1;
                else
                    $is_important = 0;
            }

            if($row['tmp_project_source_ids'] )
            {
                $tmp_project_source_ids = explode(",",$row['tmp_project_source_ids']);
                $result = array_intersect($major_activity,$tmp_project_source_ids);
                // var_dump($result);
                if($result)
                    $is_important = 1;
                else
                    $is_important = 0;
            }

            // if($row['status'] == $status )
            //     return false;
            // else
            // {

            //     $row->status = $status;
            //     $row->save();
            // }




            $is_save = 0;
            if( $progress_status_id > 0 && $row['progress_status_id'] < $progress_status_id){
                $row->progress_status_id = $progress_status_id;
                $row->progress_status_name = $progress_status_name;
                $is_save++;
                 // $row->save();
            }

            if($row['status'] != $status )
            {
                $row->status = $status;
                $is_save++;
                // $row->save();

            }
            if($row['reject_status'] != $reject_status)
            {
                $row->reject_status = $reject_status;
                $is_save++;
                // $row->save();
            }

            if($row['is_important'] != $is_important)
            {
                $row->is_important = $is_important;
                $is_save++;
                // $row->save();
            }


            if($row['required_text'] != $required_text)
            {
                $row->required_text = $required_text;
                $is_save++;

            }
            if($is_save > 0)
                $row->save();
            return false;

        });



        self::beforeUpdate(function($data){
            if(isset($data['foreign_capital'])){
                $asset_type_id = $data['foreign_capital']>0?2 : 1;
                $data['asset_type_id'] = $asset_type_id;
                $data['asset_type_name'] = $data['foreign_capital']>0?'外资' : '内资';
            }
            else{
                $data['asset_type_id'] = 1;
                $data['asset_type_name'] = '内资';
            }
        });
    }


    public function getAccessTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['access_time']) ? $data['access_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getSignTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_time']) ? $data['sign_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    public function getIntentionTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['intention_time']) ? $data['intention_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    public function getTmpIntentionTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_intention_time']) ? $data['tmp_intention_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    public function getFrameworkAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['framework_agreement_time']) ? $data['framework_agreement_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getInvesmentAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['invesment_agreement_time']) ? $data['invesment_agreement_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getRegisterTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['register_time']) ? $data['register_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getStartConstructionTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_construction_time']) ? $data['start_construction_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTmpAccessTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_access_time']) ? $data['tmp_access_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getTmpSignTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_sign_time']) ? $data['tmp_sign_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getTmpFrameworkAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_framework_agreement_time']) ? $data['tmp_framework_agreement_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getTmpInvesmentAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_invesment_agreement_time']) ? $data['tmp_invesment_agreement_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getTmpRegisterTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_register_time']) ? $data['tmp_register_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getTmpStartConstructionTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_start_construction_time']) ? $data['tmp_start_construction_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    protected function setAccessTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSignTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setIntentionTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setTmpIntentionTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    protected function setFrameworkAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setInvesmentAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRegisterTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setStartConstructionTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpAccessTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpSignTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpFrameworkAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpInvesmentAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpRegisterTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setLandTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpLandTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setTmpStartConstructionTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setFirstRegistrationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setTmpFirstRegistrationTimeAttr($value)
    {
        var_dump($value);
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setSecondRegistrationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setTmpSecondRegistrationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    protected function setFinalConfirmTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setTmpFinalConfirmTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setRewardApplicationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setTmpRewardApplicationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getCarrierIdRelationUnitAttr($value, $data)
    {
        $value = $data['carrier_id'];
        $relation_unit= '';
        switch ($value) {
            case '1':
            case '6':
                $relation_unit = '亩';
                break;
            case '2':
            case '3':
            case '4':
                $relation_unit = '平米';
                break;
            default:
                $relation_unit = '';
                break;
        }
        return $relation_unit;
    }
    public function getTmpCarrierIdRelationUnitAttr($value, $data)
    {
        $value = $data['tmp_carrier_id'];
        $relation_unit= '';
        switch ($value) {
            case '1':
            case '6':
                $relation_unit = '亩';
                break;
            case '2':
            case '3':
            case '4':
                $relation_unit = '平米';
                break;
            default:
                $relation_unit = '';
                break;
        }
        return $relation_unit;
    }
    public function managers() {
        return $this->hasMany('AdminProject','project_id');
    }


}
